﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using System.Xml.Serialization;

namespace MedReg.Classes
{
    [Serializable]
    [Table (Name = "Nomenclature")]
    public class Nomenclature : ISerializable
    {
        private long idField;

        private long parentField;

 //       private EntitySet<LPU> _lpu = new EntitySet<LPU>();

        private string nameField;

        private static readonly Nomenclature theOneObject = new Nomenclature();

        public static Nomenclature GetNomeclature()
        {
            return theOneObject;
        }
/*        
        [Association(Name = "Nomenclature_LPU_FK1", Storage = "_lpu", ThisKey = "ID", OtherKey = "NomenId")]
        public EntitySet<LPU> LPU
        {
            get
            {
                return _lpu;
            }
            set
            {
                _lpu.Assign(value);
            }
        }
        
*/        
        /// <remarks/>
        [Column(IsPrimaryKey = true)]
        public long ID
        {
        
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        //[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        [Column]
        public long Parent
        {
            get
            {
                return this.parentField;
            }
            set
            {
                this.parentField = value;
            }
        }

        /// <remarks/>
        [Column]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
       public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("ID", idField);
            info.AddValue("Parent", parentField);
            info.AddValue("Name",nameField);
        }
        /*
        public Nomenclature (SerializationInfo info, StreamingContext context)
        {
            idField = (int) info.GetValue("ID", typeof (int));
            parentField = (int?) info.GetValue("Parent", typeof (int?));
            nameField = (string)info.GetValue("Name", typeof(string));
        }
         
        public  Nomenclature (int ID, int? Parent, string Name)
        {
            this.idField = ID;
            this.parentField = Parent;
            this.nameField = Name;
        }
        */
        public  Nomenclature ()
        {
        }
        public List<Classes.Nomenclature> getNomeclatureList(Classes.ArrayOfStaffEnrty staffEntryCollection)
        {

            var sLpu = new List<Classes.LPU>(from sc in staffEntryCollection select sc.UZ);

            var sNomen = new List<Classes.Nomenclature>(from nm in sLpu select nm.Nomen);

            var ssSub = sNomen
                .GroupBy(s => new { s.ID})
                .Select(g => g.First())
                .ToList();
            return ssSub;
        }

        public List<Classes.Nomenclature> getNomeclatureList(List<Classes.LPU> sLpu)
        {

           var sNomen = new List<Classes.Nomenclature>(from nm in sLpu select nm.Nomen);

            var ssSub = sNomen
                .GroupBy(s => new { s.ID })
                .Select(g => g.First())
                .ToList();
            return ssSub;
        }

    }
    
}
